package controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;

import domain.Authentication;
import domain.Movie;
import domain.User;

public class Controller {
	private User user;
	
	public Controller(){
		this.user = new User();
	}
	
	public synchronized void logIn(String email, String password) throws SQLException{
		Authentication authen = new Authentication();
		if(authen.authentication(email, password)){
			user =  User.userLogIn(email, password);
		}
	}
	
	public synchronized void registerUser(String name, String surname, String email, String password) throws Exception{
		user.setName(name);
		user.setSurname(surname);
		//user.setUserId(7);
		user.setEmail(email);
		user.setPassword(password);
		User.registerUser(user);
		user =  User.userLogIn(email, password);
	}
	
	public synchronized Movie searchMovie(String movieName) throws SQLException{
		return Movie.search(movieName);
	}
	
	public synchronized void giveRating(int movieId, int value) throws SQLException{
		user.rateMovie(movieId, value);
	}
	
	public synchronized List<RecommendedItem> getRecommend(int howManyRecommendation) throws TasteException{
		return user.getRecommend(howManyRecommendation);
	}
	
	public synchronized Movie searchMovieById(int movieId) throws SQLException{
		return Movie.searchById(movieId);
		
	}
	
	public synchronized List<Movie> getSurveyMovies() throws SQLException{
		List<Movie> surveyMovies = new ArrayList<Movie>();
		for (int i = 1; i < 6; i++) {
			surveyMovies.add(Movie.searchById(i));
		}
		return surveyMovies;
		
	}
	
	public synchronized List<String> getRatedMovies() throws SQLException{
		return user.getUserRatedMovies();
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}
